package com.lynn.salary.web.action;

import java.util.HashMap;
import java.util.Map;

import com.lynn.salary.entity.Article;
import com.lynn.salary.entity.ArticleType;
import com.lynn.salary.pojo.FeedBack;
import com.lynn.salary.service.IArticleTypeService;
import com.lynn.salary.util.ObjectFactory;

public class ArticleTypeAction extends BaseAction {

	private ArticleType articleType;
	private String flag;
	private FeedBack feedBack;
	private IArticleTypeService articleTypeService = (IArticleTypeService) ObjectFactory.getBean("articleTypeService");
	private String[]ids;
	public String create()throws Exception{
		articleType = new ArticleType();
		articleType.setName("新建类型");
		articleType = articleTypeService.save(articleType);
		
		return "showEditPage";
	}
	
	public String edit() throws Exception {
		if(flag == null || flag.trim().equals("")) {
			articleType = articleTypeService.getByPk(articleType.getId(), ArticleType.class);
			return "showEditPage";
		}
		
		Map<String,Object> data = new HashMap<String, Object>();
		data.put("name", articleType.getName());
		data.put("showOrder", articleType.getShowOrder());
		data.put("id", articleType.getId());
		boolean f;
		try {
			f = articleTypeService.update(data, ArticleType.class);
		} catch (Exception e) {
			e.printStackTrace();
			f = false;
		}
		
		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		if (f) {
			feedBack.setMessage("编辑成功");
		} else {
			feedBack.setMessage("编辑失败");
		}
		feedBack.setRel("editArticleTypeDialog");
		feedBack.setNavTabId("allArticleType");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("closeCurrent");
		
		return "feedback";
	}
	
	public String delete() throws Exception {
		boolean  f = false;
		if(ids == null) {
			f = false;
		} else {
			try {
				f = articleTypeService.delete(ids, new ArticleType());
			} catch (Exception e) {
				e.printStackTrace();
				f = false;
			}
		}
		
		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		if (f) {
			feedBack.setMessage("删除成功");
		} else {
			feedBack.setMessage("删除失败");
		}
		feedBack.setRel("");
		feedBack.setNavTabId("allArticleType");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("");
		
		return "feedback";
	}
	
	public String publish() throws Exception {
		boolean flag;
		try {
			flag = articleTypeService.removeTag(ids, ArticleType.DRAFT, new ArticleType());
			if (flag)
				flag = articleTypeService.addTag(ids, ArticleType.PUBLISH, new ArticleType());
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}

		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		if (flag) {
			feedBack.setMessage("发布成功");
		} else {
			feedBack.setMessage("发布失败");
		}
		feedBack.setRel("");
		feedBack.setNavTabId("allArticleType");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("");

		return "feedback";
	}

	public String undoPublish() throws Exception {
		boolean flag;
		try {
			flag = articleTypeService.removeTag(ids, ArticleType.PUBLISH, new ArticleType());
			if (flag)
				flag = articleTypeService.addTag(ids, ArticleType.DRAFT, new ArticleType());
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}

		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		if (flag) {
			feedBack.setMessage("撤销成功");
		} else {
			feedBack.setMessage("撤销失败");
		}
		feedBack.setRel("");
		feedBack.setNavTabId("allArticleType");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("");

		return "feedback";
	}
	
	@Override
	public String execute() throws Exception {
		return super.execute();
	}
	public ArticleType getArticleType() {
		return articleType;
	}
	public void setArticleType(ArticleType articleType) {
		this.articleType = articleType;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public FeedBack getFeedBack() {
		return feedBack;
	}

	public void setFeedBack(FeedBack feedBack) {
		this.feedBack = feedBack;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	
}
